package DP;

import com.sun.nio.sctp.AssociationChangeNotification;

/**
 * @author chenhong
 * @version 1.0
 * @description TODO
 * @date 2023/4/19 14:08
 */
public class L30 {
    public int longestValidParentheses(String s) {
        int left = 0, right = 0, ans = 0;
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == ')')
                right++;
            if(s.charAt(i) == '(')
                left++;
            if(right == left){
                ans = Math.max(ans,left + right);
            }else if(left > right) {
                ans = Math.max(ans, right * 2);
            }else {
                left =0;
                right = 0;
            }
        }
        return  ans;
    }
}
